package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.model.dto.CourseTeacherDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author 86150
* @description 针对表【course_teacher(课程-教师关系表)】的数据库操作Service实现
* @createDate 2023-07-11 20:26:12
*/
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher>
    implements CourseTeacherService{

    @Resource
    private CourseTeacherMapper courseTeacherMapper;
    
    @Resource
    private CourseBaseMapper courseBaseMapper;

    @Override
    public List<CourseTeacher> queryCourseTeacher(Long courseId) {
        // 拼装查询条件
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseId);
        // 查询课程教师信息表
        return courseTeacherMapper.selectList(queryWrapper);
    }

    @Override
    public CourseTeacher addCourseTeacher(Long companyId, CourseTeacherDto courseTeacherDto) {
        Long courseId = courseTeacherDto.getCourseId();
        CourseBase courseBase = courseBaseMapper.selectById(courseId);

        // 只允许向自己机构课程中添加老师
        if (!companyId.equals(courseBase.getCompanyId())){
            XueChengPlusException.cast("只允许向自己机构课程中添加老师");
        }
        
        CourseTeacher courseTeacher = new CourseTeacher();
        // 拷贝addCourseTeacherDto到courseTeacher
        BeanUtils.copyProperties(courseTeacherDto,courseTeacher);
        courseTeacher.setCreateDate(LocalDateTime.now());
        // 插入数据
        int insert = courseTeacherMapper.insert(courseTeacher);
        if (insert <= 0) {
            XueChengPlusException.cast("添加教师失败");
        }
        return courseTeacher;
    }

    @Override
    public CourseTeacher modifyCourseTeacher(Long companyId,CourseTeacherDto courseTeacherDto) {
        Long courseId = courseTeacherDto.getCourseId();
        CourseBase courseBase = courseBaseMapper.selectById(courseId);

        // 只允许向自己机构课程中修改老师
        if (!companyId.equals(courseBase.getCompanyId())){
            XueChengPlusException.cast("只允许向自己机构课程中修改老师");
        }

        // 拷贝editCourseTeacherDto到courseTeacher
        CourseTeacher courseTeacher = new CourseTeacher();
        BeanUtils.copyProperties(courseTeacherDto, courseTeacher);

        // 修改课程教师表数据
        courseTeacherMapper.updateById(courseTeacher);

        return courseTeacher;
    }

    @Override
    public void deleteCourseTeacher(Long companyId,Long courseId, Long id) {
        CourseBase courseBase = courseBaseMapper.selectById(courseId);

        // 只允许向自己机构课程中删除老师
        if (!companyId.equals(courseBase.getCompanyId())){
            XueChengPlusException.cast("只允许向自己机构课程中删除老师");
        }

        // 拼装查询条件
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseId);
        queryWrapper.eq(CourseTeacher::getId,id);

        int delete = courseTeacherMapper.delete(queryWrapper);
        if (delete <= 0){
            XueChengPlusException.cast("删除教师失败");
        }
    }
}




